package com.lyf.core.data.net;


import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.lyf.core.BuildConfig;
import com.lyf.core.common.BaseConstant;

import java.net.Proxy;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitFactory {

    private Interceptor mInterceptor;

    private Retrofit mRetrofit;

    private RetrofitFactory() {
        init();
    }

    private static final class Holder {
        private static final RetrofitFactory mRetrofitFactory = new RetrofitFactory();
    }


    public static RetrofitFactory getInstance() {
        return Holder.mRetrofitFactory;
    }

    private void init() {
        mInterceptor = chain -> {
            Request.Builder builder = chain.request().newBuilder()
                    .addHeader("Content-Type", "application/json;charset=UTF-8")
                    .addHeader("Accept-Charset", "UTF-8")
                    .addHeader("Device-type", "Android")
                    .addHeader("channelName", SPUtils.getInstance().getString(BaseConstant.CHANNEL))
                    .addHeader("App-version", String.valueOf(AppUtils.getAppVersionCode()));


            String token = SPUtils.getInstance().getString(BaseConstant.TOKEN);

            if (BuildConfig.DEBUG) {
                LogUtils.d("------------------okhttp Request Head  token---------------- :" + token);
            }
            if (!StringUtils.isEmpty(token)) {
                builder.addHeader("token", token);
            }
            Request request = builder.build();
            return chain.proceed(request);
        };

        //Retrofit实例化
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .client(initClient())
                .build();
    }

    private OkHttpClient initClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(initLogInterceptor())
                .addInterceptor(mInterceptor)
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .dns(new ApiDns());
        if (!BuildConfig.DEBUG) {
            builder.proxy(Proxy.NO_PROXY);
        }
        return builder.build();
    }

    private Interceptor initLogInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
        return interceptor;
    }

    public <T> T create(Class<T> service) {
        return mRetrofit.create(service);
    }

}
